$(function () {
    getuserinfo();

    $('.logout').on('click', function () {
        layer.confirm('确认退出登陆?', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token')
            location.href = '/login.html'
            layer.close(index);
        });
    })
})

function getuserinfo() {
    $.ajax({
        type: 'GET',
        url: '/my/userinfo',
        // headers: {
        //     Authorization: localStorage.getItem('token')
        // },
        success: function (res) {
            if (res.status !== 0) {
                return layui.layer.msg(res.messig)
            }
            renderAvatar(res.data);
        },
        // complete: function (res) {
        //     if (res.responseJSON.status === 1 && res.responseJSON.message === '身份认证失败！') {
        //         localStorage.removeItem('token')
        //         location.href = '/login.html'
        //     }
        // }
    })
}
function renderAvatar(user) {
    const name = user.nickname || user.username;
    $("#welcome").html(`欢迎&nbsp;&nbsp;${name}`);
    if (user.user_pic !== null) {
        $('.layui-nav-img').prop('src', user.user_pic).show();
        $('.text-avatar').hide()
    } else {
        const first = name[0].toUpperCase();
        const secend = name[1].toUpperCase();
        $('.layui-nav-img').hide();
        $('.text-avatar').html(first + secend).show();

    }
}